package com.webtest.core;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

import com.webtest.utils.ReadProperties;

import freemarker.template.Configuration;
import freemarker.template.Template;
import jakarta.activation.DataHandler;
import jakarta.activation.DataSource;
import jakarta.activation.FileDataSource;
import jakarta.mail.Authenticator;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.Multipart;
import jakarta.mail.PasswordAuthentication;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeBodyPart;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.internet.MimeMultipart;

public class WebTestListener1 extends TestListenerAdapter {

	@Override
	public void onFinish(ITestContext testContext) {
		int success_len;
		// TODO Auto-generated method stub
		// super.onFinish(testContext);
		// 打印出所有的测试用例数目
		ITestNGMethod[] methods = this.getAllTestMethods();
		System.out.println("一共执行了：" + methods.length);

		// 成功的/失败的测试用例名称和数目
		List<ITestResult> failList = this.getFailedTests();
		int len = failList.size();
		System.out.println("失败的测试用例：" + len);
		for (int i = 0; i < len; i++) {
			ITestResult tr = failList.get(i);
			System.out
					.println(tr.getInstanceName() + ":" + tr.getName() + "失败了");

		}
		success_len = methods.length - len;

		// freemarker
		try {
			this.freemarker(methods.length, len, success_len);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		try {
			this.sendEmail("1196076920@qq.com", "测试", "测试");
		} catch (MessagingException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void freemarker(int length, int len, int success_len)
			throws Exception {
		// TODO Auto-generated method stub
		String dir = "D:\\practical training\\auto2021-1\\template";
		Configuration configuration = new Configuration();
		configuration.setDirectoryForTemplateLoading(new File(dir));
		Template template = configuration.getTemplate("/freeemarkerDemo1.ftl");

		Map root = new HashMap();
		root.put("cur_time", new Date());
		root.put("all_num", "执行了" + length + "条用例");
		root.put("fail_num", "失败了" + len + "条用例");
		root.put("success_num", "成功了" + success_len + "条用例");

		Writer out = new FileWriter(dir + "/freemarkerDemo1.html");
		template.process(root, out);
		System.out.println("----------");
		System.out.println("转换成功");
		out.flush();
		out.close();
		System.out.println("----------");

	}

	public void sendEmail(String toEmail, String subject, String body)
			throws MessagingException, IOException {
		// TODO Auto-generated method stub
		String from = ReadProperties.getPropertyValue("send");
		String auth_code = ReadProperties.getPropertyValue("auth_code");

		Properties props = new Properties();
		props.put("mail.transport.protocol", "smtp");
		props.put("mail.smtp.host", "smtp.163.com");
		props.put("mail.smtp.auth", true);
		props.put("mail.smtp.port", "25");
		System.out.println("执行发送邮件");

		Session session = Session.getInstance(props, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(from, auth_code);
			}
		});

		Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress(from));
		message.addRecipient(Message.RecipientType.TO,
				new InternetAddress(toEmail));
		message.setSubject(subject);
		message.setText(body);
		MimeBodyPart messageBodyPart2 = new MimeBodyPart();
		String filename = ReadProperties.getPropertyValue("filename");
		DataSource source = new FileDataSource(filename);
		messageBodyPart2.setDataHandler(new DataHandler(source));
		messageBodyPart2.setFileName(filename);
		Multipart multipart = new MimeMultipart();
		multipart.addBodyPart(messageBodyPart2);
		message.setContent(multipart);

		// 发送邮件
		Transport.send(message);
		System.out.println("发送成功");

	}
}
